package com.woniu.shop.controller;


import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.shop.entity.dto.CashOutDTO;
import com.woniu.shop.entity.dto.EditShopMesDTO;
import com.woniu.shop.entity.dto.ShopLoginDTO;
import com.woniu.shop.pojo.Shop;
import com.woniu.shop.pojo.ShopAppraise;
import com.woniu.shop.pojo.ShopIncome;
import com.woniu.shop.entity.dto.ShopDTO;
import com.woniu.shop.pojo.vo.ShopAuditVo;
import com.woniu.shop.service.IShopAppraiseService;
import com.woniu.shop.service.IShopIncomeService;
import com.woniu.shop.pojo.Shop;
import com.woniu.shop.pojo.dto.ShopDTO;
import com.woniu.shop.service.IShopService;
import com.woniu.util.RegularUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 门店表 前端控制器
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "门店控制类")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private IShopIncomeService ShopIncomeService;

    @Autowired
    private IShopAppraiseService shopAppraiseService;

    @PostMapping("/login")
    @ApiOperation("门店登录")
    public Result<Object> shopLogin(@RequestBody ShopLoginDTO shopLoginDTO){
        if(StringUtils.isEmpty(shopLoginDTO.getAccount()) || StringUtils.isEmpty(shopLoginDTO.getLoginPwd())){
            return Result.error("请正确填写用户名和密码");
        }
        Map<String, Object> claims = shopService.shopLogin(shopLoginDTO);
        if(claims == null){
            return Result.error(ResultCode.USER_LOGIN_ERROR.getCode(), ResultCode.USER_LOGIN_ERROR.getMessage());
        }
        return Result.ok(claims);
    }

    @PostMapping("/edit/message")
    @ApiOperation("编辑门店信息")
    public Result<Object> editShopMessage(@RequestBody EditShopMesDTO editShopMesDTO){
        if(!RegularUtil.regularPhoneNumber(editShopMesDTO.getShopPhone())){
            return Result.error("手机号格式不正确，请重新输入");
        }
        int row = shopService.editShopMessage(editShopMesDTO);
        if(row == 0){
            return Result.error("保存失败");
        }
        return Result.ok();
    }

    @PostMapping("/cash/out")
    @ApiOperation("提现")
    public Result<Object> cashOut(@RequestBody CashOutDTO cashOutDTO){
        return shopService.cashOut(cashOutDTO);
    }

    @ApiOperation("搜索+分页")
    @PostMapping("/list/search/{currentPage}/{pageSize}")
    public Result<PageResult<Shop>> querySearch(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            ShopDTO shopDTO){
        PageResult<Shop> pageResult = shopService.querySearch(currentPage, pageSize, shopDTO);
        return Result.ok(pageResult);
    }

    @ApiOperation("查询所有门店收益")
    @GetMapping("/findAll/shopIncome")
    public Result<List<ShopIncome>> queryAllShopIncome(){
        List<ShopIncome> shopIncomeList = ShopIncomeService.list();
        return Result.ok(shopIncomeList);
    }

    @ApiOperation("查询所有门店评价")
    @GetMapping("/findAll/shopAppraise")
    public Result<List<ShopAppraise>> queryAllShopAppraise(){
        List<ShopAppraise> shopAppraiseList = shopAppraiseService.list();
        return Result.ok(shopAppraiseList);
    }

    @ApiOperation("审核门店分")
    @PostMapping("/Audit/{id}")
    public Result<ShopAuditVo> queryAudit(@PathVariable int id){
        ShopAuditVo shopAuditVo = shopAppraiseService.queryAudit(id);
        return Result.ok(shopAuditVo);
    }

}
